/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.updater.display
{
	import mx.core.UIComponent;
	import sweezy.anim.core.IPropertyAnimation;
	import sweezy.anim.core.IUpdater;
	import sweezy.anim.updater.UpdaterProperty;

	public class UIComponentUpdater extends DisplayObjectUpdater
	{

		public function UIComponentUpdater()
		{
		}

		override public function update(element:Object, property:UpdaterProperty, prefix:String, fraction:Number, option:Object, target:IPropertyAnimation):void
		{
			var obj:UIComponent = element as UIComponent;
			var x:Number;
			var y:Number;
			var width:Number;
			var height:Number;
			var value:Number;

			while (property !== null)
			{
				value = property.start + (property.end - property.start) * fraction;
				value = property.round ? ((value * 2 | 0) - (value | 0)) : value;

				property.name === "x" ? x = value :
					property.name === "y" ? y = value :
					property.name === "alpha" ? obj.alpha = value :
					property.name === "width" ? width = value :
					property.name === "height" ? height = value :
					property.name === "rotation" ? obj.rotation = value :
					property.name === "scaleX" ? obj.scaleX = value :
					property.name === "scaleY" ? obj.scaleY = value :
					property.name === "z" ? obj.z = value :
					property.name === "rotationX" ? obj.rotationX = value :
					property.name === "rotationY" ? obj.rotationY = value :
					property.name === "rotationZ" ? obj.rotationZ = value :
					property.name === "scaleZ" ? obj.scaleZ = value :
					obj[property.name] = value;

				property = property.next;
			}

			if (!isNaN(x) || !isNaN(y))
			{
				x = isNaN(x) ? obj.x : x;
				y = isNaN(y) ? obj.y : y;
				obj.move(x, y);
			}

			if (!isNaN(width) || !isNaN(width))
			{
				width = isNaN(width) ? obj.width : width;
				height = isNaN(height) ? obj.height : height;
				obj.setActualSize(width, height);
			}
		}
	}
}
